package cn.zjweu.dao.impl;

import cn.zjweu.dao.ManagerDao;
import cn.zjweu.domain.Manager;
import cn.zjweu.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ManagerDaoImpl implements ManagerDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;

    private void initThree() throws SQLException {
        conn = JDBCUtil.getConnection();
        pst = null;
        rs = null;
    }

    private void initTwo() throws SQLException {
        conn = JDBCUtil.getConnection();
        pst = null;
    }

    @Override
    public Manager findAdByNameAndPWD(String username, String password) {
        Manager manager = null;
        String sql = "SELECT * FROM manager WHERE username = ? AND password = ?";

        try {
            initThree();
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setString(2, password);
            System.out.println(pst.toString());
            rs = pst.executeQuery();
            while (rs.next()) {
                if (rs.getString("username") != null) {
                    manager = new Manager();
                    manager.setUsername(rs.getString("username"));
                    manager.setPassword(rs.getString("password"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return manager;
    }
}
